import { Component, OnInit, ViewChild } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';
import { ActivatedRoute } from '@angular/router';
import { orderStateArrs } from '../../love-recyc-manage.pipe'
import { CommonService } from '../../../../services-common/common.service';
import { LovemakeupDescentService } from '../services/love-makeup-descent.service'

@Component({
  selector: 'app-love-makeup-promotion-details',
  templateUrl: './love-makeup-promotion-details.component.html',
  styleUrls: ['./love-makeup-promotion-details.component.scss']
})
export class LoveMakeupPromotionDetailsComponent implements OnInit {
  @ViewChild('userInfoRef') userInfoRef: any;

  routeParams: any = {};
  detailDatas: any = {};
  useStatistics: any = {};
  orderTableParams: any = {
    loading: false,
    state: '',
    tableData: [],
    page: 1,
    pageSize: 10,
    total: 0
  };
  
  // 订单状态options
  orderStateOptions: any = [...orderStateArrs];

  // 订单来源
  orderSourceOptions: any = [];
  // 推广渠道
  extensionOptions: any = [];
  // 用户信息
  modalUserId: any = null;

  // 查看机型Tree
  isModelVisible = false;
  defaultModelKeys: any = [];

  constructor(
    private message: NzMessageService,
    private activatedRoute: ActivatedRoute,
    private commonService: CommonService,
    private services: LovemakeupDescentService
  ) {
    activatedRoute.queryParams.subscribe((res: any) => {
      this.routeParams = res || {};
    })
  }

  ngOnInit(): void {
    this.getChannelList();
    this.loadDetailData();
    this.loadRecordTable();
  }

  // 查询订单来源
  async getChannelList() {
    this.orderSourceOptions = await this.commonService.getDictList('channel');
    this.extensionOptions = await this.commonService.getDictList('iDiy_extension_channel');
  }

  async loadDetailData() {
    let params = {
      id: this.routeParams.id
    }
    const messId = this.message.loading('数据加载中..', { nzDuration: 0 }).messageId;
    const {code, data, message}: any = await this.services.getDescentDetail(params);
    this.message.remove(messId);

    if (code === 0) {
      const {couponUserRecordIPage, promotionInfo} = data || {};
      this.detailDatas = promotionInfo || {};
      this.useStatistics = data || {};
    } else {
      this.message.error(message);
    }
  }

  // 使用记录Table List
  async loadRecordTable() {
    let params = {
      activity: this.routeParams.id,
      page: this.orderTableParams.page,
      pageSize: this.orderTableParams.pageSize
    }
    this.orderTableParams.loading = true;
    const {code, data, message}: any = await this.services.getDescentRecordList(params);
    this.orderTableParams.loading = false;
    
    if (code === 0) {
      const {records, total} = data || {};
      this.orderTableParams.tableData = records || [];
      this.orderTableParams.total = total || 0;
    } else {
      this.message.error(message);
    }
  }

  // PageIndex Change
  onPageIndexChange(index: number) {
    this.orderTableParams.page = index;
    this.loadDetailData();
  }

  // PageSize Change
  onPageSizeChange(sizeNum: number) {
    this.orderTableParams.page = 1;
    this.orderTableParams.pageSize = sizeNum;
    this.loadDetailData();
  }

  statusChange(val: any) {
    this.orderTableParams.page = 1;
    this.loadDetailData();
  }

  modelToText(str: string) {
    if (str == '0') {
      return '全部';
    } else if (str) {
      return str.split(',').length;
    } else {
      return '-';
    }
  }

  /**
   * ----------------------------------------------------------------
   * 查看指定机型 Modal
   * ----------------------------------------------------------------
   */
  selectMoreModel() {
    this.defaultModelKeys = [];
    this.defaultModelKeys = this.mergeTreeKey();
    this.isModelVisible = true;
  }

  mergeTreeKey() {
    let modelArr = (this.detailDatas.containsModel && this.detailDatas.containsModel != '0') ? this.detailDatas.containsModel.split(',') : [];
    return [...modelArr].map((item: any) => +item);
  }

  strToArray(str: string) {
    let arr = str ? str.split(',') : [];
    return arr.length;
  }

  // 订单来源filter
  filterPlatform(platform: number) {
    let tempObj = this.orderSourceOptions.find((item: any) => {
      return item.content == platform;
    })
    return tempObj ? tempObj.name : '-'
  }

  // 爱思用户信息 Modal
  userInfoPreview(item: any) {
    this.modalUserId = item.user_id;
    this.userInfoRef.isVisible = true;
  }
}
